package com.zero2oneblog.queue;

import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserSubscriber {

    @Autowired
    private AmqpAdmin amqpAdmin;

    public void subscribe(String author, String username) {
        Queue userQueue = new Queue(username, false, false, true);
        amqpAdmin.declareQueue(userQueue);

        Binding binding = BindingBuilder.bind(userQueue).to(new DirectExchange("article_exchange")).with(author);
        amqpAdmin.declareBinding(binding);
    }
}

